###############################################
# Meta-analysis #2: Leadership
###############################################

# 0) User paths  edit these
PATH <- ""  

path_meta_leadership        <- file.path(PATH, "Datasets", "meta.leadership.csv")
path_meta_leadership_biv    <- file.path(PATH, "Datasets", "meta.leadership_biv.csv")
path_meta_leadership_random <- file.path(PATH, "Datasets", "meta.leadership_random.csv")
path_out_csv                <- file.path(PATH, "Results",  "2.results_leadership_models.csv")

# 1) Settings
outcome_name <- "Leadership"

# 2) Packages
suppressPackageStartupMessages({
  library(metafor)
})

# 3) Load data
meta.leadership        <- read.csv(path_meta_leadership,        stringsAsFactors = FALSE)
meta.leadership_biv    <- read.csv(path_meta_leadership_biv,    stringsAsFactors = FALSE)
meta.leadership_random <- read.csv(path_meta_leadership_random, stringsAsFactors = FALSE)

# 4) Basic checks
required_cols <- c("authoryear", "yi", "vi")
if (!all(required_cols %in% names(meta.leadership))) {
  stop("meta.leadership is missing required columns: ",
       paste(setdiff(required_cols, names(meta.leadership)), collapse = ", "))
}
if (!all(required_cols %in% names(meta.leadership_biv))) {
  stop("meta.leadership_biv is missing required columns: ",
       paste(setdiff(required_cols, names(meta.leadership_biv)), collapse = ", "))
}
if (!all(required_cols %in% names(meta.leadership_random))) {
  stop("meta.leadership_random is missing required columns: ",
       paste(setdiff(required_cols, names(meta.leadership_random)), collapse = ", "))
}

# 5) Full, all  three level RE
full_all_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.leadership),
  data   = meta.leadership,
  test   = "t"
)
full_all_df <- data.frame(
  coef        = unname(full_all_fit$beta),
  ci.lb95     = full_all_fit$ci.lb,
  ci.ub95     = full_all_fit$ci.ub,
  pval        = full_all_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.leadership$authoryear)),
  k           = nrow(meta.leadership),
  sample_type = "All",
  model_type  = "Full",
  stringsAsFactors = FALSE
)

# 6) Bivariate, all  three level RE
biv_all_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.leadership_biv),
  data   = meta.leadership_biv,
  test   = "t"
)
biv_all_df <- data.frame(
  coef        = unname(biv_all_fit$beta),
  ci.lb95     = biv_all_fit$ci.lb,
  ci.ub95     = biv_all_fit$ci.ub,
  pval        = biv_all_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.leadership_biv$authoryear)),
  k           = nrow(meta.leadership_biv),
  sample_type = "All",
  model_type  = "Bivariate",
  stringsAsFactors = FALSE
)

# 7) Full, random  three level RE
full_rand_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.leadership_random),
  data   = meta.leadership_random,
  test   = "t"
)
full_rand_df <- data.frame(
  coef        = unname(full_rand_fit$beta),
  ci.lb95     = full_rand_fit$ci.lb,
  ci.ub95     = full_rand_fit$ci.ub,
  pval        = full_rand_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.leadership_random$authoryear)),
  k           = nrow(meta.leadership_random),
  sample_type = "Quasi-experimental",
  model_type  = "Full",
  stringsAsFactors = FALSE
)

# 8) Combine and save
out_models <- rbind(full_all_df, biv_all_df, full_rand_df)

print(out_models, row.names = FALSE)

if (!is.null(path_out_csv)) {
  write.csv(out_models, path_out_csv, row.names = FALSE)
}
